<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <link rel="stylesheet" type="text/css" th:href="@{/static/bootstrap/css/bootstrap.min.css}"/>
	<link rel="stylesheet" type="text/css" th:href="@{/static/css/normalize.css}"/>
	<link rel="stylesheet" type="text/css" th:href="@{/static/css/rightCon.css}"/>
	<link rel="stylesheet" type="text/css" th:href="@{/static/css/page.css}"/>
	<link rel="stylesheet" type="text/css" th:href="@{/static/css/thesame.css}"/>
	<link rel="stylesheet" type="text/css" th:href="@{/static/user/css/popUp.css}"/>
    
    <style type="text/css">
        .table_operation span{cursor: pointer}
        .error{color:red}
        /*取消选中效果*/
		div{
	    -moz-user-select:none;/*火狐*/
	    -webkit-user-select:none;/*webkit浏览器*/
	    -ms-user-select:none;/*IE10*/
	    -khtml-user-select:none;/*早期浏览器*/
	      user-select:none;
		}
		.msgs{ opacity: 0.4; }
		.push-ul{ border-bottom:1px solid #ddd; padding:0 30px }
		.push-ul li{ width: 120px; float:left }
		.push-ul li span{ display: inline-block; padding:20px 0 }
		.push-ul li span.current{ border-bottom: 2px solid #2A58AB  }
		span.wyfzrRegion{
			width: 150px;
			display: inline-block;
			border: 1px solid #1677FF;
			border-radius: 5px;
			text-align: center;
			margin: 10px;
			height: 30px;
			line-height: 30px;
			cursor: pointer;
		}
		span.wyfzrRegion.current{
			color: #fff;
			background-color: #1677FF;
		}
		span.wyfzrRegion.noSelect{
			color: #fff;
			background-color: #ccc;
			border: none;
			cursor: default;
		}
    </style>
</head>
<script th:inline="javascript">var basePath = [[${#httpServletRequest.getContextPath()}]];</script>
<body>
<input id="initPage" th:value="${initPage}" type="hidden">
<div class="right-Sidebar-con">
	<article class="mb20px">
	<div class="pad20px overflow box-shadows bgWhite border-radius5 mb20px">
		<ul class="form-inline account-form textCenter overflow push-ul">
			<li style="cursor: pointer" class="userType"><span class="current" data-val="1" style="font-weight:700">系统管理员</span></li>
			<li style="cursor: pointer" class="userType"><span data-val="2" style="font-weight:700">物业负责人</span></li>
		</ul>
	</div>
	</article>
	<article class="mb20px">
	<div class="pad20px overflow box-shadows bgWhite border-radius5 mb20px">
		<ul class="form-inline account-form">
			<li class="form-group mr20px">
				<p class="form-group"><label>登录账号</label></p>
				<p class="form-group"><input type="text" id="loginName" class="form-control" maxlength="12" placeholder="请输入登陆账号"></p>
			</li>
			<li class="form-group form-inline mr20px">
				<p class="form-group"><label>用户姓名</label></p>
				<p class="form-group"><input type="text" id="userName" class="form-control" maxlength="30" placeholder="请输入姓名"></p>
			</li>
			<li class="form-group mr20px">
				<p class="form-group"><label>手机号码</label></p>
				<p class="form-group"><input type="text" id="phone" class="form-control" maxlength="11" placeholder="请输入手机号码"></p>
			</li>
			<li class="form-group lh34px">
				<span class="bgBlue white inline-block border-radius3 pl30px pr30px mr20px" id="queryBtn">查  询</span>
				<span class="bgWhite border1px border-box inline-block border-radius3 pl30px pr30px" id="resetBtn">重  置</span>
			</li>
		</ul>
	</div>
    <div class="overflow box-shadows bgWhite border-radius5">
        <div class="pad20px overflow">
			<span id="addUser" class="bgBlue white inline-block border-radius3 pl15px pr15px lh30px"><img th:src="@{/static/img/tj.png}" class="mr10px">创建账号</span>
			<span class="right">共有<em id="totalCount" class="red">120</em>条数据</span>
		</div>
        <div class="table-con">
            <table cellpadding="0" cellspacing="0" width="100%" border="0" class="textCenter" id="main_table">
            	<thead>
                <tr>
                    <th style="width: 12%">账号</th>
                    <th style="width: 12%">姓名</th>
                    <th style="width: 12%">所属组织</th>
                    <th style="width: 12%">部门/职务</th>
                    <th style="width: 10%">手机号码</th>
                    <th style="width: 10%">最近登录时间</th>
                    <th style="width: 10%">状态</th>
                    <th style="width: 22%">操作</th>
                </tr>
                </thead>
               <tbody>

               </tbody>
            </table>
            <div class="textRight page-con">
                <ul class="page inline-block" maxshowpageitem="5" pagelistcount="10"  id="page"></ul>
            </div>
        </div>
    </div>
    </article>
</div>

<div id="pop-delete-con-confirm" style="display:none;">
	<div class="popUp" style="padding:30px;">
		<div class="delete-con">
			<ul>
				<li style="font-size: 17px;font-weight: bold;"><img th:src="@{/static/img/tc_icon_xz.png}">是否要删除此数据？</li>
				<li class="f14px mt15px">删除后将无法恢复，请确认是否要删除这条数据</li>
			</ul>
			<div class="btn-con textRight pt15px cursor">
				<div class="inline-block textCenter overflow">
					<p class="confirm-cancel mr20px close-btn">取    消</p>
					<p class="confirm-con">确    定(5)</p>
				</div>
			</div>
		</div>
	</div>
</div> 

<div id="pop-ret-con-fail" style="display:none;">
	<div class="popUp" style="padding:30px;">
		<div class="delete-con">
			<dl class="textCenter">
				<dt><img th:src="@{/static/img/tc_icon_sb.png}"></dt>
				<dd id="pop-ret-con-fail-msg" class="red"></dd>
			</dl>
		</div>
	</div>
</div> 

<div id="pop-ret-con-fail2" style="display:none;">
	<div class="popUp" style="padding:30px;">
		<div class="delete-con">
			<dl class="textCenter">
				<dt><img th:src="@{/static/img/tc_icon_sb.png}"></dt>
				<dd id="pop-ret-con-fail-title2" class="pt20px pb20px f18px fb"></dd>
				<dd id="pop-ret-con-fail-msg2" class="red"></dd>
			</dl>
		</div>
	</div>
</div> 

<div id="pop-ret-con-succ" style="display:none;">
	<div class="popUp"  style="padding:30px;">
		<div class="delete-con">
			<dl class="textCenter">
				<dt><img th:src="@{/static/img/tc_icon_cg.png}"></dt>
				<dd id="pop-ret-con-succ-msg" class="pt20px f18px fb"></dd>
			</dl>
		</div>
	</div>
</div>

<script th:src="${#request.getContextPath()}+'/static/js/jquery-2.1.3.min.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/layer/layer.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/js/page.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/js/date.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/validation/jquery.validate.js'"></script>
<script>
	var pageSize = 5;
    var currentPage = 1;
    var dataCount = 0;
    var companyList;

    jQuery.validator.addMethod("alnum", function(value, element) {
        return this.optional(element) || /^[a-zA-Z0-9]{0,20}$/.test(value);
    }, "只支持英文字母和数字！");
    jQuery.validator.addMethod("isMobile",function(value, element) {
			var length = value.length;
			var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
			return this.optional(element) || (length == 11 && mobile
				.test(value));
		}, "请正确填写您的手机号码");

    $(function(){
        initEvent();
        var initPage = $("input#initPage").val();
        if(initPage){
            getData(initPage);
        }else {
            getData(1);
        }
    })
    
    //查询按钮
    $("#queryBtn").unbind().click(function(){
    	getData(1);
    })
    
    //重置按钮
    $("#resetBtn").unbind().click(function(){
    	$("#userName").val(null).html("");
    	$("#loginName").val(null).html("");
    	$("#phone").val(null).html("");

    })

    //查询操作员列表
    function getData(page){
    	var userName=$("#userName").val();
    	var loginName=$("#loginName").val();
    	var phone=$("#phone").val();
		var userType = $("li.userType span.current").data('val');
        $.ajax({
            url:basePath+"/user/getUserList",
            type:"post",
            dataType:"json",
            data:{"currentPage":page,"pageSize":pageSize,"userName":userName,"loginName":loginName,"phone":phone,"userType":userType},
            success:function (result) {
                var html = "";
                if(result.code == 200){
                    if(result.data && result.data.records && result.data.records.length > 0){
                        $.each(result.data.records,function (i,v) {
                        	html += "<tr>";
                        	html += "<td style='width: 12%'>"+v.LOGIN_NAME+"</td>";
                            html += "<td style='width: 12%'>"+v.USER_NAME+"</td>";
                            html += "<td style='width: 12%'>"+v.ORG_NAME+"</td>";
                            html += "<td style='width: 12%'>"+v.POST+"</td>";
                            html += "<td style='width: 12%'>"+v.PHONE+"</td>";
                            var login_time = ((v.LOGIN_TIME==null||v.LOGIN_TIME=='')? "":formatTime(v.LOGIN_TIME));
                            html += "<td style='width: 10%'>"+login_time+"</td>";
                            if(v.STATUS=='0'){
                            	html += "<td style='width: 10%;color:red;'>"+v.STATUS_NAME+"</td>";
                            }else{
                            	html += "<td style='width: 10%'>"+v.STATUS_NAME+"</td>";
                            }
                            html += "<td><p class='lightGray account-btn'>";
                            if(v.STATUS=='1' && v.LOGIN_TIME==null){
                            	html +="<span class='blue deleteUser' data-id='"+v.USER_NO+"'>删除</span>";
                            }else if(v.STATUS=='0' && v.LOGIN_TIME!=null){
                            	html +="<span class='blue openUser' data-id='"+v.USER_NO+"'>开通</span>";
                            }else if(v.STATUS=='1' && v.LOGIN_TIME!=null){
                            	html +="<span class='blue closeUser' data-id='"+v.USER_NO+"'>停用</span>";
                            }
                            html += "|<span class='blue editUser' data-id='"+v.USER_NO+"'>编辑</span>";
                            //if(v.POST_TYPE == "1" || v.POST_TYPE == 1) {
								html += "|<span class='blue authUser' data-no='" + v.USER_NO + "'>权限配置</span>";
							//}
                            html += "</p></td>";
                            html += "</tr>";
                        })
                        $("#page").initPage(result.data.total,result.data.current,result.data.pages,getData,result.data.size);
						$("#page").show();
                        currentPage = result.data.current;
                        dataCount = result.data.records.length;
                    }else{
                        html += "<tr><td colspan='7'>暂无用户信息！</td></tr>";
						$("#page").hide();
                    }
                }else{
                    html += "<tr><td colspan='7'>获取用户列表失败！</td></tr>";
					$("#page").hide();
                }
                $("table#main_table tbody").html(html);
                $("#totalCount").html(result.data.total);
                $("span.jurisdiction").unbind().on("click",function (e) {
                    var id = $(this).data("id");
                    window.location.href = basePath+"/user/userMenu?userNo="+id+"&userPage="+currentPage;
                })
                $("span.resetPassword").unbind().on("click",function (e) {
                    var id = $(this).data("id");
                    layer.confirm('是否确认重置密码为 111111 ？', {
                        btn: ['确定','取消']
                    }, function(){
                        layer.closeAll();
                        resetPassword(id);
                    });
                })
                $("span.editUser").unbind().on("click",function (e) {
                    var id = $(this).data("id");
					var userType = $("li.userType span.current").data('val');
					if(userType == "1" || userType == 1) {
						layerOpen(id);
					}else{
						wyLayerOpen(id)
					}
                })
                $("span.deleteUser").unbind().on("click",function (e) {
                     var id = $(this).data("id");
                    //  layer.confirm('是否确认删除该用户？', {
                    //     btn: ['确定','取消']
                    // }, function(){
                    //     layer.closeAll();
                    //     deleteUser(id);
                    // });
					$("#pop-delete-con-confirm").data("id",id);
					deletePop(this);
                })
                $("span.openUser").unbind().on("click",function (e) {
                     var id = $(this).data("id");
                     openUser(id);
					// layer.confirm('是否确认开通该用户？', {
                    //     btn: ['确定','取消']
                    // }, function(){
                    //     layer.closeAll();
                    //     openUser(id);
                    // });
                })
                $("span.closeUser").unbind().on("click",function (e) {
					 var userType = $("li.userType span.current").data('val');
                     var id = $(this).data("id");
                     closeUser(id,userType);
                    // layer.confirm('是否确认停用该用户？', {
                    //     btn: ['确定','取消']
                    // }, function(){
                    //     layer.closeAll();
                    //     closeUser(id);
                    // });
                })
                $("span.authUser").unbind().on("click",function (e) {
					var no = $(this).data("no");
                   	window.location.href=basePath+"/user/auth?userNo="+no;
                })
            }
        })
    }

    function getUserById(id,scope) {
        $.ajax({
            url:basePath+"/user/getUserById",
            type:"post",
            dataType:"json",
            data:{"userNo":id},
            success:function (result) {
                if(result.code == 200){
                    $("input[name=userNo]",scope).val(result.data.USER_NO);
                    $("input[name=loginName]",scope).val(result.data.LOGIN_NAME);
                    $("input[name=loginName]",scope).attr("readonly","readonly");
                    $("input[name=userName]",scope).val(result.data.USER_NAME);
                    $("input[name=phone]",scope).val(result.data.PHONE);
                    $("input[name=post]",scope).val(result.data.POST);
                    if(result.data.POST_TYPE == "1" || result.data.POST_TYPE == 1) {
						$("#selectParentOrg").val(result.data.ORG_ID).text(result.data.ORG_NAME);
					}else{
						$("#selectParentOrg").val(result.data.ORG_ID).trigger("change");
						if(result.data.regions && result.data.regions.length > 0){
							$.each(result.data.regions,function (i,v) {
								$("span#regions_"+v.region).removeClass("noSelect").addClass("current");
							})
							$("span.wyfzrRegion:not(.noSelect)").unbind().on("click",function (e) {
								if($(this).hasClass("current")){
									$(this).removeClass("current");
								}else{
									$(this).addClass("current");
								}
							})
						}
                    }
                    $("#userSave").html("修改");
                }else{
                    layer.closeAll();
                    layer.msg(result.msg);
                }
            }
        })
    }

    function resetPassword(id) {
        $.ajax({
            url:basePath+"/user/resetPassword",
            type:"post",
            dataType:"json",
            data:{"userNo":id},
            success:function (result) {
                layer.msg(result.msg);
                if(result.code == 200){
                    getData(currentPage)
                }
            }
        })
    }

    function deleteUser(id) {
    	var userType = $("li.userType span.current").data('val');
        $.ajax({
            url:basePath+"/user/delete",
            type:"post",
            dataType:"json",
            data:{"userNo":id,"postType":userType},
            success:function (result) {
                if(result.code == 200){
                	retSuccPop("删除成功");
                    if(dataCount > 1) {
                        getData(currentPage);
                    }else{
                        if(currentPage > 1){
                            getData(parseInt(currentPage) - 1);
                        }else{
                            getData(currentPage);
                        }
                    }
                }else{
                	retFailPop("删除失败");
                }
            }
        })
    }
    
    function openUser(id) {
        $.ajax({
            url:basePath+"/user/open",
            type:"post",
            dataType:"json",
            data:{"userNo":id},
            success:function (result) {
                layer.msg(result.msg);
                if(result.code == 200){
                	retSuccPop("启用成功");
                    if(dataCount > 1) {
                        getData(currentPage);
                    }else{
                        if(currentPage > 1){
                            getData(parseInt(currentPage) - 1);
                        }else{
                            getData(currentPage);
                        }
                    }
                }else{
                	retFailPop("启用失败");
                }
            }
        })
    }
    
    function closeUser(id,userType) {
        $.ajax({
            url:basePath+"/user/close",
            type:"post",
            dataType:"json",
            data:{"userNo":id,"postType":userType},
            success:function (result) {
                if(result.code == 200){
                	retSuccPop("停用成功");
                    if(dataCount > 1) {
                        getData(currentPage);
                    }else{
                        if(currentPage > 1){
                            getData(parseInt(currentPage) - 1);
                        }else{
                            getData(currentPage);
                        }
                    }
                }else{
                	retFailPop("停用失败");
                }
            }
        })
    }
    
    function initEvent() {
        $("#addUser").on("click",function (e) {
			var userType = $("li.userType span.current").data('val');
			if(userType == "1" || userType == 1) {
				layerOpen();
			}else if(userType == "2" || userType == 2){
				wyLayerOpen();
			}
        })

		$("li.userType").on("click",function (e) {
			if(! $(this).find("span").hasClass("current")) {
				$(this).find("span").addClass("current");
				$(this).siblings().find("span").removeClass("current");
				getData(1);
			}
		})

		$.ajax({
			url:basePath+"/user/getCompanyAll",
			type:"post",
			dataType:"json",
			success:function (result) {
				if(result.code == 200){
					companyList = result.data;
				}else{
					retFailPop(result.msg)
				}
			}
		})
    }
    
    /**
     * 添加选择机构-机构
     */
    function addOrgSelect(obj){
    	var name = $(obj).html();
    	var orgId = $(obj).data("orgid");	
    	$("#selectParentOrg").text(name).val(orgId);
    	$('.add-has-one ul').hide();
    	$('.tree-con').slideUp(500);
    }


    function layerOpen(id) {
        var layerIndex = layer.open({
            type: 1,
            title: false,
            closeBtn: false,
            shade: 0.6,
            //skin: 'layui-layer-rim', //加上边框
            area: ['700px', '80%'], //宽高
            content: layerHtml,
            success:function(){
            	var str="";
            	$.ajax({
            		type:"post",
            		url:basePath+"/org/getAllOrg",
            		dataType:"json",		
            		success:function(data){
            			var str="";
            			$.each(data.children,function(index,ao){
            				str+='<p onclick="addOrgSelect(this);event.cancelBubble =true" class="gray pb10px border-bot" data-orgid="'+ao.orgId+'">'+ao.orgName+'</p>';
            				if(ao.isChildExist=='1'){
            					str+='<ul class="organ-con add-has-one">';
            					$.each(ao.children,function(index,ao1){
            						str+='<li>';
            						str+='<div class="organ-list has-btn overflow-clear">';
            						if(ao1.isAdminInstitute=='0'){
            							str+='<div style="width:300px"><span class="first"><img src="'+basePath+'/static/img/arrow_down.png"></span><em style="color:#ccc">'+ao1.orgName+'</em></div>';
            						}else{
            							str+='<div style="width:300px"><span class="first"><img src="'+basePath+'/static/img/arrow_down.png"></span><em onclick="addOrgSelect(this);event.cancelBubble =true" data-orgid="'+ao1.orgId+'">'+ao1.orgName+'</em></div>';
            						}
            						str+='</div>';
            						if(ao1.isChildExist=='1'){
            							str+='<ul class="organ-con add-has-two">';
            							$.each(ao1.children,function(index,ao2){
            								str+='<li>';
            								str+='<div class="organ-list has-btn overflow-clear">';
            								str+='<div style="width:300px">';
            								if(ao2.isChildExist=='1'){
            									str+='<span class="first"><img src="'+basePath+'/static/img/arrow_down.png"></span>';
            								}
            								if(ao2.isAdminInstitute=='1'){
            									str+='<em onclick="addOrgSelect(this);event.cancelBubble =true" data-orgid="'+ao2.orgId+'">'+ao2.orgName+'</em>';
            								}else{
            									str+='<em style="color:#ccc;">'+ao2.orgName+'</em>';
            								}
            								
            								str+='</div>'
            								str+='</div>';
            								if(ao2.isChildExist=='1'){
            									str+='<ul class="organ-con add-has-three">';
            									$.each(ao2.children,function(index,ao3){
            										str+='<li>';
            										str+='<div class="organ-list has-btn overflow-clear">';
            										str+='<div style="width:300px">';
            										if(ao3.isChildExist=='1'){
            											str+='<span class="first"><img src="'+basePath+'/static/img/arrow_down.png"></span>';
            										}
            										if(ao3.isAdminInstitute=='1'){
                    									str+='<em onclick="addOrgSelect(this);event.cancelBubble =true" data-orgid="'+ao3.orgId+'">'+ao3.orgName+'</em>';
                    								}else{
                    									str+='<em style="color:#ccc;">'+ao3.orgName+'</em>';
                    								}
            										str+='</div>'
            										str+='</div>';
            										if(ao3.isChildExist=='1'){
            											str+='<ul class="organ-con add-has-four">';
            											$.each(ao3.children,function(index,ao4){
            												str+='<li>';
            												str+='<div class="organ-list has-btn overflow-clear">';
            												str+='<div style="width:300px">';
            												if(ao4.isChildExist=='1'){
            													str+='<span class="first"><img src="'+basePath+'/static/img/arrow_down.png"></span>';
            												}
            												if(ao4.isAdminInstitute=='1'){
                            									str+='<em onclick="addOrgSelect(this);event.cancelBubble =true" data-orgid="'+ao4.orgId+'">'+ao4.orgName+'</em>';
                            								}else{
                            									str+='<em style="color:#ccc;">'+ao4.orgName+'</em>';
                            								}
            												str+='</div>'
            												str+='</div>';
            												str+='</li>';
            											})
            											str+='</ul>'
            										}
            										str+='</li>';
            									})
            									str+='</ul>';
            								}
            								str+='</li>';
            							})	
            							str+='</ul>';
            						}
            						str+='</li>';
            					});
            					str+='</ul>';
            				}
            				str+='';
            				
            				
            			});
            			$(".tree-con").html(str);
            			$('.tree-con div.has-btn .first, .deploy-fold div.has-btn').click(function(){
            				$(this).parent().parent().next('').slideToggle(300);
            				$(this).parent().parent().parent().siblings().find('ul').slideUp(300);
            				
            				if( $(this).find('span.first img').attr('src') == basePath+'/static/img/arrow_down.png'){
            					$(this).find('span.first img').attr('src',basePath+'/static/img/arrow_up.png')
            					$(this).parent().siblings().find('span.orange img').attr('src',basePath+'/static/img/arrow_down.png')
            				}
            				else{
            					$(this).find('span.first img').attr('src',basePath+'/static/img/arrow_down.png')
            				}
            			})	 
            			
            			$('.tree-con').hide();
            			$('.add-has-one ul').hide();
            			$('.select-click').click(function(){
            				$('.tree-con').slideToggle();
            			})
            			
            			$(".close-btn").click(function(){
            				layer.closeAll();	
            			});
            			
            		}
            	})
    			
                var scope = $("div#layerHtmlDiv");
                var form = $("form#userForm",scope);
                if(id){
                    getUserById(id,scope);
                }
                /* $(form).validate(
                    {
                        rules:{
                            loginName:{required:true,maxlength:20,alnum:true},
                            userName:{required:true,maxlength:10},
                            phone:{required:true,maxlength:11,isMobile: true},
                            selectParentOrg:{required:true}
                        },
                        messages:{
                            loginName:{required:"用户账号不能为空！",maxlength:"用户账号最多20位！"},
                            userName:{required:"用户姓名不能为空！",maxlength:"用户姓名最多10个字符！"},
                            phone:{required:"手机号不能为空！",maxlength:"手机号最多11位！",isMobile: "请正确填写您的手机号码！"},
                            selectParentOrg:{required:"上层组织机构不能为空！"}
                        }
                    }
                    
                ) */

                $(".close-btn",scope).unbind().on("click",function(e){
                    layer.closeAll();
                });

                $("span#userSave").unbind().on("click",function (e) {
					var userType = $("li.userType span.current").data('val');
                    var userNo = $("input[name=userNo]").val();
                    var loginName = $("input[name=loginName]").val();
                    var userName = $("input[name=userName]").val();
                    var phone = $("input[name=phone]").val();
                    var post = $("input[name=post]").val();
                    var selectParentOrg = $("#selectParentOrg").val();
                    if(isNull(loginName)){
                    	layer.msg("用户账号不能为空！");
                    	return;
                    }
                    if(isNull(userName)){
                    	layer.msg("用户姓名不能为空！");
                    	return;
                    }
                    if(isNull(selectParentOrg)){
                    	layer.msg("上级组织不能为空！");
                    	return;
                    }
                    if(isNull(phone)){
                    	layer.msg("手机号不能为空！");
                    	return;
                    }
                    if (!/^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(phone)){
                    	layer.msg("请正确填写您的手机号码！");
                    	return;
            		}

                    $.ajax({
                    url:basePath + "/user/save",
                    type:"post",
                    dataType:"json",
                    data:{"userNo":userNo,"userName":userName,"loginName":loginName,"phone":phone,"post":post,"orgId":selectParentOrg,"userType":userType},
                    beforeSend: function(){
           				$("#userSave").css("pointer-events", "none");
            		},
                    success:function (result) {
                    	var suffix=(userNo==null||userNo==''?"添加":"修改");
                    	if(result.code==200){
                    		retSuccPop(suffix+"成功");
                    	}else{
                    		retFailPop2(suffix+"失败", result.msg);
                    	}
                        getData(1);
                        $("#authBtn").css("pointer-events", "auto");
                    }
                })
                    
                })
            }
        });
    }

	function wyLayerOpen(id) {
		var layerIndex = layer.open({
			type: 1,
			title: false,
			closeBtn: false,
			shade: 0.6,
			area: ['700px', '80%'], //宽高
			content: getWyLayerHtml(),
			success:function(){
				$("select#selectParentOrg").on("change",function (e) {
					var companyId = $(this).val();
					if(companyId){
						$.ajax({
							url:basePath+"/user/getCompanyRegion",
							type:"post",
							dataType:"json",
							data:{"companyId":companyId},
							async:false,
							success:function (result) {
								if(result.code == 200){
									var html = "";
									if(result.data && result.data.length > 0){
										$.each(result.data,function (i,v) {
											if(v.flag == "1" || v.flag == 1){
												html += "<span id='regions_"+v.region+"' class='wyfzrRegion noSelect' data-val='"+v.region+"'>"+v.org_name+"</span>";
											}else{
												html += "<span id='regions_"+v.region+"' class='wyfzrRegion' data-val='"+v.region+"'>"+v.org_name+"</span>";
											}
										})
									}
									$("div#wuyefuzerenRegionDiv").html(html);
									$("span.wyfzrRegion:not(.noSelect)").unbind().on("click",function (e) {
										if($(this).hasClass("current")){
											$(this).removeClass("current");
										}else{
											$(this).addClass("current");
										}
									})
								}else{
									layer.msg(result.msg);
								}
							}
						});
					}else{
						$("div#wuyefuzerenRegionDiv").html("");
					}
				});

				$(".close-btn",scope).unbind().on("click",function(e){
					layer.closeAll();
				});

				var scope = $("div#layerHtmlDiv");
				var form = $("form#userForm",scope);
				if(id){
					getUserById(id,scope);
				}

				$(".close-btn",scope).unbind().on("click",function(e){
					layer.closeAll();
				});

				$("span#userSave").unbind().on("click",function (e) {
					var userType = $("li.userType span.current").data('val');
					var userNo = $("input[name=userNo]").val();
					var loginName = $("input[name=loginName]").val();
					var userName = $("input[name=userName]").val();
					var phone = $("input[name=phone]").val();
					var post = $("input[name=post]").val();
					var selectParentOrg = $("#selectParentOrg").val();
					var regions = "";
					$("span.wyfzrRegion.current").each(function (i,v) {
						var val = $(this).data("val");
						if(val){
							regions += val +",";
						}
					})
					if(regions){regions.substring(0,regions.length - 1);}
					if(isNull(loginName)){
						layer.msg("用户账号不能为空！");
						return;
					}
					if(isNull(userName)){
						layer.msg("用户姓名不能为空！");
						return;
					}
					if(isNull(selectParentOrg)){
						layer.msg("物业公司不能为空！");
						return;
					}
					if(isNull(regions)){
						layer.msg("负责区域不能为空！");
						return;
					}
					if(isNull(phone)){
						layer.msg("手机号不能为空！");
						return;
					}
					if (!/^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(phone)){
						layer.msg("请正确填写您的手机号码！");
						return;
					}

					$.ajax({
						url:basePath + "/user/save",
						type:"post",
						dataType:"json",
						data:{"userNo":userNo,"userName":userName,"loginName":loginName,"phone":phone,"post":post,"orgId":selectParentOrg,"regions":regions,"userType":userType},
						beforeSend: function(){
							$("#userSave").css("pointer-events", "none");
						},
						success:function (result) {
							var suffix=(userNo==null||userNo==''?"添加":"修改");
							if(result.code==200){
								retSuccPop(suffix+"成功");
							}else{
								retFailPop2(suffix+"失败", result.msg);
							}
							getData(1);
							$("#authBtn").css("pointer-events", "auto");
						}
					})
				})
			}
		});
	}

    function getNotNull(val) {
        val = val + "";
        if($.trim(val) && "null" != val){
            return val;
        }else{
            return "";
        }
    }
    
    function isNull(value) {
        return value == null || value === "" || value === undefined;
    }  
    
   
    
    /**
     * 提示失败
     */
    function retFailPop(msg){
    	$("#pop-ret-con-fail-msg").html(msg);
    	//$("#pop-ret-con-fail-msg").html(msg);
    	layer.closeAll();
    	layer.open({
            type: 1  
            ,title: false //不显示标题栏
            ,closeBtn: false
            ,area: ['25%', '']
            ,shade: 0.8
            ,id: 'deleteFailPop' //设定一个id，防止重复弹出
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,shadeClose: true //设置允许点遮罩关闭
            ,content: $("#pop-ret-con-fail").html(),
            success:function(){
            },
            
        });
      }
    
    /**
     * 提示失败，显示具体原因
     */
    function retFailPop2(title, msg){
    	$("#pop-ret-con-fail-title2").html(title);
    	$("#pop-ret-con-fail-msg2").html(msg);
    	layer.closeAll();
    	layer.open({
            type: 1  
            ,title: false //不显示标题栏
            ,closeBtn: false
            ,area: ['25%', '']
            ,shade: 0.8
            ,id: 'deleteFailPop2' //设定一个id，防止重复弹出
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,shadeClose: true //设置允许点遮罩关闭
            ,content: $("#pop-ret-con-fail2").html(),
            success:function(){
            },
            
        });
      }
      
    /**
     * 提示成功
     */
    function retSuccPop(msg){
    	$("#pop-ret-con-succ-msg").html(msg);
    	layer.closeAll();
    	layer.open({
            type: 1  
            ,title: false //不显示标题栏
            ,closeBtn: false
            ,area: ['25%', '']
            ,shade: 0.8
            ,id: 'deleteSuccPop' //设定一个id，防止重复弹出
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,shadeClose: true //设置允许点遮罩关闭
            ,content: $("#pop-ret-con-succ").html(),
            success:function(){
            },
            
        });
      }
    
    /**
     * 删除弹层
     */
    function deletePop(obj){
    	var id = $(obj).data("id");
		var userType = $("li.userType span.current").data('val');
        layer.open({
            type: 1  
            ,title: false //不显示标题栏
            ,closeBtn: false
            //是否要删除此数据
            ,area: ['30%', '']
            //新建组织
            //,area: ['40%','']
            ,shade: 0.8
            ,id: 'deletePop' //设定一个id，防止重复弹出
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,shadeClose: false //设置允许点遮罩关闭
            ,content: $("#pop-delete-con-confirm").html(),
            success:function(){
            	//是否要删除此数据
            	var time=5;
    			var code=$(".delete-con .confirm-con");
    			code.addClass("msgs");
    			code.html("确  定"+"("+time+")");
    			var interval=setInterval(function() {
    				time--;
    				code.html("确  定"+"("+time+")");
    				if (time==0) {
    					clearInterval(interval);
    					code.html("确  定");
    					code.removeClass("msgs");
    					code.attr("onclick","deleteUser('"+id+"')");
    				}
    			},1000)
    			//关闭弹层
    			$(".close-btn").click(function(e){
    				var code=$(".delete-con .confirm-con");
    				code.html("确    定(5)");
    				clearInterval(interval);
    				interval=null;
    				var code=$(".delete-con .confirm-con");
    				code.removeAttr("onclick");
    				layer.closeAll();
    			});
            },
            
        });
      }
    

    var layerHtml = '<div class="popUp" id="layerHtmlDiv">\n' +
        '<ul class="pop-ul textCenter f18px fb relative">\n' +
        '<li><span>用户信息</span></li>\n' +
        '<p><span class="close-btn"><img src="'+basePath+'/static/img/close.png"></span></p>\n' +
        '</ul>\n' +
        '<div class="form-con">\n' +
        '<form class="form-horizontal" id="userForm" role="form">\n' +
        '<input type="hidden" name="userNo" id="userNo" /> '+
        '<div class="form-group row">\n' +
        '<label class="col-sm-3 control-label"><span class="red">* </span>用户账号</label>\n' +
        '<div class="col-sm-9">\n' +
        '<input type="text" name="loginName" class="form-control" maxlength="20" autocomplete="off" placeholder="请输入用户账号">\n' +
        '</div>\n' +
        '</div>\n' +
        '<div class="form-group row">\n' +
        '<label class="col-sm-3 control-label"><span class="red">* </span>用户姓名</label>\n' +
        '<div class="col-sm-9">\n' +
        '<input type="text" name="userName" class="form-control" maxlength="10" autocomplete="off" placeholder="请输入用户姓名">\n' +
        '</div>\n' +
        '<div class="form-con">\n' +
        '</div>\n' +
        '<label style="margin-top: 15px" class="col-sm-3 control-label"><span class="red">* </span>所属组织</label>\n' +
        '<div class="col-sm-9">\n' +
        '<p id="selectParentOrg" class="form-control form-select gray select-click" style="margin-top: 15px;color:#555555">请选择</p>\n' +
        '<nav class="tree-con pad20px border-radius5 box-shadows mt10px mb10px">\n' +
        '</nav>\n' +
        '</div>\n' +
        '</div>\n' +
        '<div class="form-group row">\n' +
        '<label class="col-sm-3 control-label"><span class="red">* </span>手机号</label>\n' +
        '<div class="col-sm-9">\n' +
        '<input type="text" name="phone" class="form-control" maxlength="11" autocomplete="off" placeholder="请输入手机号">\n' +
        '</div>\n' +
        '</div>\n' +
        '<div class="form-group row">\n' +
        '<label class="col-sm-3 control-label">部门/职务</label>\n' +
        '<div class="col-sm-9">\n' +
        '<input type="text" name="post" class="form-control" maxlength="30" autocomplete="off" placeholder="请输入部门职务">\n' +
        '</div>\n' +
        '</div>\n' +
        
        '</form>\n' +
        '<div class="btn-bot textCenter">\n' +
        '<p class="inline-block textCenter overflow">\n' +
        '<span class="bgBlue white" id="userSave">保存</span><span class="bgGray close-btn">取消</span>\n' +
        '</p>\n' +
        '</div>\n' +
        '</div>\n' +
        '</div>';


    function getWyLayerHtml() {
		var wyLayerHtml = '<div class="popUp" id="layerHtmlDiv">\n' +
				'<ul class="pop-ul textCenter f18px fb relative">\n' +
				'<li><span>用户信息</span></li>\n' +
				'<p><span class="close-btn"><img src="'+basePath+'/static/img/close.png"></span></p>\n' +
				'</ul>\n' +
				'<div class="form-con">\n' +
				'<form class="form-horizontal" id="userForm" role="form">\n' +
				'<input type="hidden" name="userNo" id="userNo" /> '+
				'<div class="form-group row">\n' +
				'<label class="col-sm-3 control-label"><span class="red">* </span>用户账号</label>\n' +
				'<div class="col-sm-9">\n' +
				'<input type="text" name="loginName" class="form-control" maxlength="20" autocomplete="off" placeholder="请输入用户账号">\n' +
				'</div>\n' +
				'</div>\n' +
				'<div class="form-group row">\n' +
				'<label class="col-sm-3 control-label"><span class="red">* </span>用户姓名</label>\n' +
				'<div class="col-sm-9">\n' +
				'<input type="text" name="userName" class="form-control" maxlength="10" autocomplete="off" placeholder="请输入用户姓名">\n' +
				'</div>\n' +
				'<div class="form-con">\n' +
				'</div>\n' +
				'<label style="margin-top: 15px" class="col-sm-3 control-label"><span class="red">* </span>物业公司</label>\n' +
				'<div class="col-sm-9">\n' +
				'<select id="selectParentOrg" class="form-control form-select gray select-click" style="margin-top: 15px;color:#555555"><option value="">请选择</option>\n';
		if(companyList && companyList.length > 0){
			$.each(companyList,function (i,v) {
				wyLayerHtml += "<option value='"+v.id+"'>"+v.name+"</option>";
			})
		}
		wyLayerHtml += '</select></div>\n' +
				'</div>\n' +
				'<div class="form-group row">\n' +
				'<label class="col-sm-3 control-label"><span class="red">* </span>负责区域</label>\n' +
				'<div class="col-sm-9" id="wuyefuzerenRegionDiv">\n' +
				'</div>\n' +
				'</div>\n' +
				'<div class="form-group row">\n' +
				'<label class="col-sm-3 control-label"><span class="red">* </span>手机号</label>\n' +
				'<div class="col-sm-9">\n' +
				'<input type="text" name="phone" class="form-control" maxlength="11" autocomplete="off" placeholder="请输入手机号">\n' +
				'</div>\n' +
				'</div>\n' +
				'<div class="form-group row">\n' +
				'<label class="col-sm-3 control-label">部门/职务</label>\n' +
				'<div class="col-sm-9">\n' +
				'<input type="text" name="post" class="form-control" maxlength="30" autocomplete="off" placeholder="请输入部门职务">\n' +
				'</div>\n' +
				'</div>\n' +

				'</form>\n' +
				'<div class="btn-bot textCenter">\n' +
				'<p class="inline-block textCenter overflow">\n' +
				'<span class="bgBlue white" id="userSave">保存</span><span class="bgGray close-btn">取消</span>\n' +
				'</p>\n' +
				'</div>\n' +
				'</div>\n' +
				'</div>';

		return wyLayerHtml;
	}

</script>
</body>
</html>
